#include <iostream>
#include "workerManager.h"
#include "boss.cpp"
#include "manager.cpp"
#include "employee.cpp"
using namespace std;
WorkerManager::WorkerManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if(!ifs.is_open())
	{
		this->empNum = 0;
		this->empArray = NULL;
		this->fileIsEmpty = true;
		ifs.close();
		cout<<"file is not exist"<<endl;
		return;
	}
	char ch;
	ifs>>ch;
	if(ifs.eof())
	{
		this->empNum = 0;
		this->empArray = NULL;
		this->fileIsEmpty = true;
		ifs.close();
		cout<<"file is empty"<<endl;
		return;
	}
	int num = this->getEmpQuantity();
	// cout<<"staff quantity is "<<num<<" at present"<<endl;
	this->empNum = num;
	this->fileIsEmpty = false;

	this->empArray = new Worker*[num];
	this->initEmp();
}
void WorkerManager::showMenu()
{
	cout<<"======================================================"<<endl;
	cout<<"||staff quantity is "<<this->empNum<<" at present                    ||"<<endl;
	cout<<"||welcome use staff management system               ||"<<endl;
	cout<<"||1.addition staff information                      ||"<<endl;
	cout<<"||2.show staff information                          ||"<<endl;
	cout<<"||3.delete quit staff                               ||"<<endl;
	cout<<"||4.modify staff information                        ||"<<endl;
	cout<<"||5.find staff information                          ||"<<endl;
	cout<<"||6.order by staff id                               ||"<<endl;
	cout<<"||7.clean all document                              ||"<<endl;
	cout<<"||8.relunch                                         ||"<<endl;
	cout<<"||0.exit management system                          ||"<<endl;
	cout<<"======================================================"<<endl;
}
void WorkerManager::exitSystem()
{
	cout<<"see you next time"<<endl;
	system("pause");
	exit(0);
}
void WorkerManager::addEmp()
{
	cout<<"please input worker quantity:";
	int addNum;
	cin>>addNum;
	if(addNum > 0)
	{
		int newSize = this->empNum + addNum;
		cout<<"old size:"<<this->empNum<<"new size"<<newSize<<endl;
		Worker ** newSpace = new Worker*[newSize];
		if(this->empArray != NULL)
		{
			for (int i = 0; i < this->empNum; ++i)
				newSpace[i] = this->empArray[i];
			for (int i = 0; i < this->empNum; ++i)
			{
				cout<<newSpace[i]->id<<"\t";
				cout<<newSpace[i]->name<<"\t";
				cout<<newSpace[i]->deptId<<endl;
			}

		}
		// input new data
		for (int i = 0; i < addNum; ++i)
		{
			int id;
			string name;
			int position;
			cout<<"please input worker id:";
			cin>>id;
			cout<<"please name:";
			cin>>name;
			cout<<"please position(1. employee, 2. manager, 3. boss):";
			cin>>position;

			Worker * wk = NULL;
			switch(position)
			{
				case 1:
					wk = new Employee(id,name,position);
					wk->showInfo();
					break;
				case 2:
					wk = new Manager(id,name,position);
					wk->showInfo();
					break;
				case 3:
					wk = new Boss(id,name,position);
					wk->showInfo();
					break;
				default:
					break;
			}
			newSpace[this->empNum + i] = wk;
			cout<<newSpace[this->empNum + i]->id<<"\t";
			cout<<newSpace[this->empNum + i]->name<<"\t";
			cout<<newSpace[this->empNum + i]->deptId<<endl;
		}
		delete [] this->empArray;
		this->empArray = newSpace;
		this->empNum = newSize;
		this->fileIsEmpty = false;
		// if(newSpace != NULL)
		// {
		// 	for (int i = 0; i < newSize; ++i)
		// 	{
		// 		delete newSpace[i];
		// 		newSpace[i] = NULL;
		// 	}
		// 	delete [] newSpace;
		// 	newSpace = NULL;
		// } 浅拷贝不能释放空间
		cout<<"addition succeed"<<endl;
		system("pause");
		this->save();
	}
	else
	{
		cout<<"error! please input again"<<endl;
	}
}
void WorkerManager::save()
{
	ofstream ofs;
	ofs.open(FILENAME, ios::out);
	for (int i = 0; i < this->empNum; ++i)
	{
		ofs<<this->empArray[i]->id<<"\t"<<this->empArray[i]->name<<"\t"<<empArray[i]->deptId<<endl;
	}
	ofs.close();
}
int WorkerManager::getEmpQuantity()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int deptId;
	int num = 0;

	while(ifs>>id && ifs>>name && ifs>>deptId)
	{
		num++;
	}
	return num;
}
void WorkerManager::initEmp()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int id;
	string name;
	int deptId;
	int row = 0;

	while(ifs>>id && ifs>>name && ifs>>deptId)
	{
		if(deptId == 1)
		{
			this->empArray[row] = new Employee(id, name, deptId);
		}
		else if(deptId == 2)
		{
			this->empArray[row] = new Manager(id, name, deptId);
		}
		else if(deptId == 3)
		{
			this->empArray[row] = new Boss(id, name, deptId);
		}
		row++;
	}
	ifs.close();
}
void WorkerManager::showEmp()
{
	if(this->fileIsEmpty)
	{
		cout<<"file is empty"<<endl;
	}
	else
	{
		for (int i = 0; i < this->empNum; ++i)
		{
			cout<<this->empArray[i]->id<<"\t";
			cout<<this->empArray[i]->name<<"\t";
			cout<<this->empArray[i]->deptId<<endl;
		}
	}
	system("pause");
}
int WorkerManager::isExist(int id)
{
	for (int i = 0; i < this->empNum; ++i)
	{
		if(this->empArray[i]->id == id)
			return i;
	}
	return -1;
}
void WorkerManager::delEmp()
{
	if(this->fileIsEmpty)
	{
		cout<<"record is empty"<<endl;
	}
	else
	{
		cout<<"please input delete id:";
		int id = 0;
		cin>>id;
		int ret = this->isExist(id);
		if(ret != -1)
		{
			for (int i = ret; i < this->empNum-1; ++i)
			{
				this->empArray[i] = this->empArray[i+1];
			}
			this->empNum--;
			this->save();
		}
		else
		{
			cout<<"delete fail, staff is not exist"<<endl;
			system("pause");
		}
	}
}
void WorkerManager::modifyEmp()
{
	if(this->fileIsEmpty)
	{
		cout<<"file is null"<<endl;
	}
	else
	{
		cout<<"please input staff id:";
		int id;
		cin>>id;
		int ret = this->isExist(id);
		if(ret != -1)
		{
			delete this->empArray[ret];
			int newId = 0;
			string name = "";
			int deptId = 0;

			cout<<"please input new id:";
			cin>>id;
			cout<<"please input new name:";
			cin>>name;
			cout<<"please input new deptId(1 or 2 or 3):";
			cin>>deptId;

			switch(deptId)
			{
				case 1: this->empArray[ret] = new Employee(id, name, deptId); break;
				case 2: this->empArray[ret] = new Manager(id, name, deptId); break;
				case 3: this->empArray[ret] = new Boss(id, name, deptId); break;
				default: break;
			}
			this->save();
			cout<<"modify succeed"<<endl;
		}
		else
		{
			cout<<"modify fail"<<endl;
		}
		system("pause");
	}
}
void WorkerManager::findEmp()
{
	if(this->fileIsEmpty)
		cout<<"file or record is empty"<<endl;
	else
	{
		cout<<"1.staff no"<<endl;
		cout<<"2.staff name"<<endl;
		cout<<"please input find method:";

		int choice = 0;
		cin>>choice;
		if(choice == 1)
		{
			// no
			int id = 0;
			cout<<"please input id:";
			cin>>id;
			int ret = isExist(id);
			if(ret != -1)
			{
				cout<<"find succeed"<<endl;
				this->empArray[ret]->showInfo();
			}
			else
			{
				cout<<"find fail"<<endl;
			}
		}
		else if(choice == 2)
		{
			// name
			cout<<"please input staff name:";
			string name;
			cin>>name;
			for (int i = 0; i < this->empNum; ++i)
			{
				if(this->empArray[i]->name == name)
				{
					cout<<"find succeed"<<endl;
					this->empArray[i]->showInfo();
				}
				else if(i == this->empNum-1)
				{
					cout<<"find fail"<<endl;
				}
			}
		}
		else
		{
			cout<<"input error"<<endl;
		}
	}
	system("pause");
}
void WorkerManager::sortEmp()
{
	if(this->fileIsEmpty)
	{
		cout<<"file or record is empty"<<endl;
	}
	else
	{
		for (int i = 0; i < this->empNum; ++i)
		{
			int min = i;
			for (int j = i+1; j < this->empNum; ++j)
			{
				if(this->empArray[min]->id > this->empArray[j]->id)
				{
					min = j;
				}
			}
			if(i != min)
			{
				Worker * temp = this->empArray[i];
				this->empArray[i] = this->empArray[min];
				this->empArray[min] = temp;
			}
		}
		cout<<"succeed"<<endl;
		this->showEmp();
	}
}
void WorkerManager::cleanFile()
{
	cout<<"1.yes"<<endl;
	cout<<"2.no"<<endl;
	cout<<"are you sure?"<<endl;
	int sure;
	cin>>sure;
	if(sure==1)
	{
		ofstream ofs(FILENAME, ios::trunc);
		ofs.close();

		if(this->empArray != NULL)
		{
			for (int i = 0; i < this->empNum; ++i)
			{
				delete this->empArray[i];
				this->empArray[i] = NULL;
			}
			delete [] this->empArray;
			this->empArray = NULL;
			this->empNum = 0;
			this->fileIsEmpty = true;
		}
		cout<<"clean succeed"<<endl;
	}
	else
	{
		cout<<"user cancel operation"<<endl;
	}
	system("pause");
}
WorkerManager::~WorkerManager()
{
	if(this->empArray != NULL)
	{
		for (int i = 0; i < this->empNum; ++i)
		{
			delete this->empArray[i];
			this->empArray[i] = NULL;
		}
		delete [] this->empArray;
		this->empArray = NULL;
	}
}
